﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OnlinePlotsService
{
    public partial class SimpleColorPicker : UserControl
    {
        public event EventHandler ColorChanged;
        public event EventHandler Clicked;

        private Color[][] COLORS = 
        {
            new Color[]{ Color.FromArgb(255, 255, 255, 255), Color.FromArgb(255, 255, 192, 192), Color.FromArgb(255, 255, 224, 192), Color.FromArgb(255, 255, 255, 192), Color.FromArgb(255, 192, 255, 192), Color.FromArgb(255, 192, 255, 255), Color.FromArgb(255, 192, 192, 255), Color.FromArgb(255, 255, 192, 255)},
            new Color[]{ Color.FromArgb(255, 224, 224, 224), Color.FromArgb(255, 255, 128, 128), Color.FromArgb(255, 255, 192, 128), Color.FromArgb(255, 255, 255, 128), Color.FromArgb(255, 128, 255, 128), Color.FromArgb(255, 128, 255, 255), Color.FromArgb(255, 128, 128, 255), Color.FromArgb(255, 255, 128, 255)},
            new Color[]{ Color.FromArgb(255, 192, 192, 192), Color.FromArgb(255, 255, 0, 0), Color.FromArgb(255, 255, 128, 0), Color.FromArgb(255, 255, 255, 0), Color.FromArgb(255, 0, 255, 0), Color.FromArgb(255, 0, 255, 255), Color.FromArgb(255, 0, 0, 255), Color.FromArgb(255, 255, 0, 255)},
            new Color[]{ Color.FromArgb(255, 128, 128, 128), Color.FromArgb(255, 192, 0, 0), Color.FromArgb(255, 192, 64, 0), Color.FromArgb(255, 192, 192, 0), Color.FromArgb(255, 0, 192, 0), Color.FromArgb(255, 0, 192, 192), Color.FromArgb(255, 0, 0, 192), Color.FromArgb(255, 192, 0, 192)},
            new Color[]{ Color.FromArgb(255, 64, 64, 64), Color.FromArgb(255, 128, 0, 0), Color.FromArgb(255, 128, 64, 0), Color.FromArgb(255, 128, 128, 0), Color.FromArgb(255, 0, 128, 0), Color.FromArgb(255, 0, 128, 128), Color.FromArgb(255, 0, 0, 128), Color.FromArgb(255, 128, 0, 128)},
            new Color[]{ Color.FromArgb(255, 0, 0, 0), Color.FromArgb(255, 64, 0, 0), Color.FromArgb(255, 128, 64, 64), Color.FromArgb(255, 64, 64, 0), Color.FromArgb(255, 0, 64, 0), Color.FromArgb(255, 0, 64, 64), Color.FromArgb(255, 0, 0, 64), Color.FromArgb(255, 64, 0, 64)}
        };

        private RadioButton _selectedColor;
        private List<RadioButton> _rbColors = new List<RadioButton>();

        public Color SelectedColor
        {
            get
            {
                return _selectedColor != null ? ((SolidColorBrush)_selectedColor.Background).Color : Colors.Transparent;
            }
            set
            {
                if (_selectedColor != null) _selectedColor.IsChecked = false;
                foreach (var rb in _rbColors)
                {
                    if (((SolidColorBrush)rb.Background).Color == value)
                    {
                        rb.IsChecked = true;
                        _selectedColor = rb;
                    }
                }
            }
        }

        public SimpleColorPicker()
        {
            InitializeComponent();
            InitializeColors();
        }

        private void InitializeColors()
        {
            StackPanel[] columns = new StackPanel[COLORS[0].Length];

            for (int i = 0; i < COLORS[0].Length; i++)
            {
                StackPanel sp = new StackPanel();
                sp.Style = (Style)Resources["ColorsColumnStyle"];
                pMain.Children.Add(sp);
                columns[i] = sp;
            }

            for (int i = 0; i < COLORS.Length; i++)
            {
                for (int j = 0; j < COLORS[i].Length; j++)
                {
                    RadioButton rb = new RadioButton();
                    rb.Style = (Style)Resources["ColorStyle"];
                    rb.Template = (ControlTemplate)Resources["ColorTemplate"];
                    rb.Background = new SolidColorBrush(COLORS[i][j]);
                    rb.Checked += new RoutedEventHandler(rb_Checked);
                    rb.Click += new RoutedEventHandler(rb_Click);
                    columns[j].Children.Add(rb);

                    _rbColors.Add(rb);
                }
            }
        }

        void rb_Click(object sender, RoutedEventArgs e)
        {
            if (Clicked != null)
            {
                Clicked(this, EventArgs.Empty);
            }
        }

        void rb_Checked(object sender, RoutedEventArgs e)
        {
            if (ColorChanged != null)
            {
                _selectedColor = (RadioButton)sender;
                ColorChanged(this, EventArgs.Empty);
            }
        }
    }
}
